/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sambi.mvc;

import java.awt.Font;
import com.sambi.mvc.FormControl;
import java.awt.KeyboardFocusManager;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Collections;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 *
 * @author Imam Habib Pamungkas <imamhabibpamungkas@gmail.com>
 */
public class TextField extends JTextField {

    private boolean required;
    private int posisi;
    private int length;

    public TextField() {
        FormControl.addtoForm(this);
        setActionCommand(getName());
        setPreferredSize(new java.awt.Dimension(20, 20));
       
        setFont(new Font("Serif", Font.BOLD, 24));
        //disable tab
        setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);

        addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    if (!(((TextField) e.getSource()).getText().equals(""))) {
                        FormControl.next();
                    } else {
                        JOptionPane.showMessageDialog(null, "Isian belum valid", "Error", JOptionPane.ERROR_MESSAGE);
                        setText("");
                    }
                }
                if ((e.getKeyCode() == KeyEvent.VK_TAB) || (e.getKeyCode() == KeyEvent.VK_DOWN)) {
                    JOptionPane.showMessageDialog(null, "Untuk next silahkan pake enter", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }

        });
        addFocusListener(new FocusAdapter() {

            @Override
            public void focusGained(FocusEvent e) {
                super.focusGained(e); //To change body of generated methods, choose Tools | Templates.
             //   FormControl.getScroller().getViewport().scrollRectToVisible(((TextField) e.getSource()).getBounds());
                FormControl.setPosisi(getPosisi());
            }

            @Override
            public void focusLost(FocusEvent e) {
                super.focusLost(e); //To change body of generated methods, choose Tools | Templates.
              //  FormControl.setPreviousText(FormControl.getListForm().get(getPosisi()));
            }

        });
        setDocument(new PlainDocument() {

            @Override
            public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
                super.insertString(offs, str, a); //To change body of generated methods, choose Tools | Templates.
                if (getLength() == length) {
                    FormControl.next();
                }
            }
        });

    }

    public boolean isRequired() {
        return required;
    }

    public void setRequired(boolean required) {
        this.required = required;
    }

    public void addToList() {

    }

    public int getPosisi() {
        return posisi;
    }

    public void setPosisi(int posisi) {
        this.posisi = posisi;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }
}
